import Koa from 'koa'
import cors from '@koa/cors'
import http from 'http';
import koaBodyparser from 'koa-bodyparser'
import webrtcRoutes from './routes/webrtc-routes.js'
import { errorHandler } from './middlewares/error-handler.js'
import { initializeSocket } from './config/socket.io.js';

const app = new Koa()
const server = http.createServer(app.callback());

initializeSocket(server);

app.use(
  cors({
    origin: '*',
    methods: ['GET', 'POST'],
    headers: ['Content-Type'],
  })
)

app.use(koaBodyparser())
app.use(errorHandler)

app.use(webrtcRoutes.routes()).use(webrtcRoutes.allowedMethods())

const PORT = process.env.PORT || 3000
server.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`)
})
